using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Deepcode.Flux.Core.Data;

namespace Example.Workflow.Objects
{
	/// <summary>
	/// The TelephoneBook class provides access to the TelephoneBook table
	/// </summary>
	public class TelephoneBook : BaseManager
	{
		#region Constructors
		public TelephoneBook() : base() { }
		public TelephoneBook(SqlConnection CurrentConnection) : base(CurrentConnection) { }
		public TelephoneBook(SqlConnection CurrentConnection, SqlTransaction CurrentTransaction) : base(CurrentConnection, CurrentTransaction) { }
		#endregion
		#region Data Retrievals
		/// <summary>
		/// Return a list of all TelephoneBookEntry records on the system.
		/// </summary>
		/// <returns></returns>
		public TelephoneBookEntry [] GetAllTelephoneBook()
		{
			DataTable results = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("egTeleGetAll");
			da.Fill(results);
			ReleaseConnection();

			List<TelephoneBookEntry> list = new List<TelephoneBookEntry>();
			foreach (DataRow r in results.Rows)
				list.Add(new TelephoneBookEntry(r));

			return list.ToArray();
		}
		/// <summary>
		/// Returns all entries being managed by the workflow definition specified.
		/// </summary>
		/// <param name="DefinitionID"></param>
		/// <returns></returns>
		public TelephoneBookEntry [] GetAllTelephoneBookForWF(int DefinitionID)
		{
			DataTable results = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("egTeleGetForWF");
			da.SelectCommand.Parameters.Add( SQLUtility.ParameterIn("@wfid", SqlDbType.Int, 4, DefinitionID));
			da.Fill(results);
			ReleaseConnection();

			List<TelephoneBookEntry> list = new List<TelephoneBookEntry>();
			foreach( DataRow r in results.Rows )
				list.Add( new TelephoneBookEntry( r ));

			return list.ToArray();
		}
		/// <summary>
		/// returns all approved telephone book entries
		/// </summary>
		/// <returns></returns>
		public TelephoneBookEntry [] GetApprovedTelephoneBook()
		{
			DataTable results = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("egTeleGetApproved");
			da.Fill(results);
			ReleaseConnection();

			List<TelephoneBookEntry> list = new List<TelephoneBookEntry>();
			foreach (DataRow r in results.Rows)
				list.Add(new TelephoneBookEntry(r));

			return list.ToArray();
		}
		/// <summary>
		/// Returns a single TelephoneBook record by it's ID number
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public TelephoneBookEntry GetTelephoneBookByID(int ID)
		{
			DataTable result = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("egTeleGetByID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			da.Fill(result);
			ReleaseConnection();

			if (result == null) return null;
			if (result.Rows.Count < 1) return null;

			return new TelephoneBookEntry(result.Rows[0]);
		}
		#endregion
		#region Save
		/// <summary>
		/// Saves the specified telephone book entry to the database.
		/// </summary>
		/// <param name="User"></param>
		/// <returns></returns>
		public int Save(TelephoneBookEntry e)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("egTeleSave");

			SqlParameter result = SQLUtility.ParameterInOut("@id", SqlDbType.Int, 4, e.PK_ID);
			cmd.Parameters.Add(result);
			cmd.Parameters.Add( SQLUtility.ParameterIn("@Name", SqlDbType.NVarChar, 500, e.Name ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@Tel", SqlDbType.NVarChar, 50, e.Tel ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@State", SqlDbType.Int, 4, e.State ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@Notes", SqlDbType.NText, 16, e.Notes ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@ApprovalHistory", SqlDbType.NText, 16, e.ApprovalHistory ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@FK_WorkflowDefinition", SqlDbType.Int, 4, e.FK_WorkflowDefinition ));
			cmd.ExecuteNonQuery();
			ReleaseConnection();

			if( e.PK_ID == -1 )
				e.PK_ID = (int) result.Value;

			return e.PK_ID;
		}
		#endregion
		#region Delete
		/// <summary>
		/// Deletes an entry by it's ID
		/// </summary>
		/// <param name="PK_ID"></param>
		/// <returns></returns>
		public bool DeleteByID(int PK_ID)
		{
			if (PK_ID == -1) return false;
			EstablishConnection();
			
			SqlCommand cmd = this.CreateSqlCommand("egTeleDelete");
			cmd.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, PK_ID));
			cmd.ExecuteNonQuery();

			ReleaseConnection();
			return true;
		}
		#endregion
	}
}
